﻿using BW.Demo.Domain.Entities;
using BW.Demo.ErrorCode;
using BW.Demo.Infrastructure.Interfcaes;
using BW.Demo.WebApi.Applications.Commands;
using MediatR;

namespace BW.Demo.WebApi.Applications.CommandHandlers
{
    public class TraceabilityBeathDeleteCommandHandler : IRequestHandler<TraceabilityBeathDeleteCommand, APIResult<int>>
    {
        IBaseRespitory<TraceabilityManagement> respitory;
        public TraceabilityBeathDeleteCommandHandler(IBaseRespitory<TraceabilityManagement> respitory)
        {
            this.respitory = respitory;
        }
        public async Task<APIResult<int>> Handle(TraceabilityBeathDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=ResultCode.删除成功;
            result.Message=ResultCode.删除成功.ToString();//["1","2"]   [1,2]
            List<int> list = request.IdString.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            var Dellist=respitory.GetAll().Where(c=>list.Contains(c.Basic_Traceability));
            foreach (var item in Dellist)
            {
                item.IsDeleted=true;
            }
            await respitory.UpdateRangeAsync(Dellist.ToList()); 
            return await Task.FromResult(result);
        }
    }
}
